package com.ruigi.control;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruigi.common.Basecontext;
import com.ruigi.pojo.R;
import com.ruigi.pojo.ShoppingCart;
import com.ruigi.service.ShopcartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShopcartControl {
    @Autowired
    private ShopcartService shopcartService;

    /**
     * 添加
     *
     * @param cart
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart cart) {
        log.info("{}", cart);

        cart.setUserId(Basecontext.getid());
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();

        if (cart.getDishId() != null) {

            queryWrapper.eq(ShoppingCart::getDishId, cart.getDishId());


        } else {
            queryWrapper.eq(ShoppingCart::getSetmealId, cart.getSetmealId());

        }
        ShoppingCart one = shopcartService.getOne(queryWrapper);
        //判断是否在数据库当中
        if (one != null) {
            Integer number = one.getNumber();
            one.setNumber(number + 1);
            shopcartService.updateById(one);
            one.setCreateTime(LocalDateTime.now());


        } else {
            cart.setNumber(1);
            cart.setCreateTime(LocalDateTime.now());
            shopcartService.save(cart);
            one = cart;
        }


        return R.success(one);


    }

    @GetMapping("/list")
    public R<List<ShoppingCart>> list() {
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<ShoppingCart>();

        queryWrapper.eq(ShoppingCart::getUserId, Basecontext.getid());
        queryWrapper.orderByAsc(ShoppingCart::getCreateTime);
        List<ShoppingCart> list = shopcartService.list(queryWrapper);
        return R.success(list);


    }

    @DeleteMapping("/clean")
    public R<String> delete() {
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, Basecontext.getid());
        shopcartService.remove(queryWrapper);
        return R.success("清空成功");

    }

    @PostMapping("/sub")
    public R<String> sub(@RequestBody ShoppingCart cart) {
        Long setmealId = cart.getSetmealId();
        Long dishId = cart.getDishId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, Basecontext.getid());

        if (dishId != null) {
            queryWrapper.eq(ShoppingCart::getDishId, dishId);

        } else {
            queryWrapper.eq(ShoppingCart::getSetmealId, setmealId);
        }
        ShoppingCart one = shopcartService.getOne(queryWrapper);
        if (one.getNumber() <= 1) {
            shopcartService.remove(queryWrapper);


        } else {
            int i = one.getNumber() - 1;
            one.setNumber(i);
            shopcartService.update(one, queryWrapper);


        }
        return R.success("成功");

    }


}
